// var n;
// var a;
// exports.LoggerImpl = exports.LogLevel = void 0;
// (a = n = exports.LogLevel || (exports.LogLevel = {}))[(a.DEBUG = 1)] = "DEBUG";
// a[(a.DUMP = 16)] = "DUMP";
// a[(a.WARN = 256)] = "WARN";
// a[(a.ERROR = 4096)] = "ERROR";
// a[(a.TIME = 65536)] = "TIME";
// a[(a.ALL = 69905)] = "ALL";
// var s = (function () {
//     function t() {
//         this.logger = console;
//         this._level = n.ALL;
//         this._timeLogMap = {};
//         this._timeLogMap = {};
//         this.update();
//     }
//     t.Instance = function () {
//         return this._instance || (this._instance = new t());
//     };
//     Object.defineProperty(t.prototype, "level", {
//         get: function () {
//             return this._level;
//         },
//         set: function (e) {
//             this._level = e;
//             this.update();
//         },
//         enumerable: !1,
//         configurable: !0
//     });
//     t.prototype.attach = function (e) {
//         if (!this.isValid(e)) {
//             this.level = this.level | e;
//             this.update();
//         }
//     };
//     t.prototype.detach = function (e) {
//         if (this.isValid(e)) {
//             this.level = this.level ^ e;
//             this.update();
//         }
//     };
//     t.prototype.isValid = function (e) {
//         return !!(this.level & e);
//     };
//     t.prototype.update = function () {
//         this.isValid(n.DUMP)
//             ? cc.sys.isBrowser
//                 ? (this.logger.dump = console.log)
//                 : (this.logger.dump = this.dump.bind(this))
//             : (this.logger.dump = function () {});
//         this.isValid(n.ERROR) ? (this.logger.e = console.error) : (this.logger.e = function () {});
//         this.isValid(n.DEBUG) ? (this.logger.d = console.log) : (this.logger.d = function () {});
//         this.isValid(n.WARN) ? (this.logger.w = console.warn) : (this.logger.w = function () {});
//         this.isValid(n.TIME)
//             ? ((this.logger.ts = this._kaishi_jishi.bind(this)), (this.logger.te = this._jiesu_jishi.bind(this)))
//             : ((this.logger.ts = function () {}), (this.logger.te = function () {}));
//     };
//     t.prototype._kaishi_jishi = function (e) {
//         this._timeLogMap[e] = Date.now();
//     };
//     t.prototype._jiesu_jishi = function (e) {
//         if (e in this._timeLogMap) {
//             var t = Date.now() - this._timeLogMap[e];
//             this.logger.d(e + " use: " + t + "ms");
//             this._timeLogMap[e] = null;
//             delete this._timeLogMap[e];
//         } else {
//             this.logger.e("log key:" + e + " is not exsist!");
//         }
//     };
//     t.prototype.dump = function () {
//         if (this.isValid(n.DUMP)) {
//             var e = this._dump(arguments[0], arguments[1], arguments[2], arguments[4]);
//             this.logger.d(e);
//         }
//     };
//     t.prototype._dump = function (t, o, i, n) {
//         if (void 0 === o) {
//             o = "unkown";
//         }
//         if (void 0 === i) {
//             i = 10;
//         }
//         if (void 0 === n) {
//             n = 0;
//         }
//         if (i < 0) {
//             return "...";
//         }
//         n += 3;
//         var a = this;
//         o = void 0 === o ? "" : o;
//         var s = "";
//         var r = "";
//         switch (typeof t) {
//             case "boolean":
//                 s += (r = o.length > 0 ? o + " = " : "") + "Boolean(1) " + (t ? "TRUE" : "FALSE");
//                 break;
//             case "number":
//                 s += (r = o.length > 0 ? o + " = " : "") + t;
//                 break;
//             case "string":
//                 s += (r = o.length > 0 ? o + " = " : "") + '"' + t + '"';
//                 break;
//             case "object":
//                 s +=
//                     (r = o.length > 0 ? o + " => " : "") +
//                     (function (e) {
//                         if (null === e) {
//                             return "NULL(0)";
//                         }
//                         var t = "";
//                         var o = 0;
//                         var s = "";
//                         if (e instanceof Array) {
//                             o = e.length;
//                             for (var r = 0; r < n; ++r) {
//                                 s += " ";
//                             }
//                             t = "Array(" + o + ") " + (s.length, "[");
//                             for (var c = 0; c < o; ++c) {
//                                 t +=
//                                     "\n" +
//                                     (0 === s.length ? "" : "" + s) +
//                                     "   [" +
//                                     c +
//                                     "] = " +
//                                     a._dump(e[c], "", i - 1, n);
//                             }
//                             return t + "\n" + (0 === s.length ? "" : "" + s) + "]";
//                         }
//                         if (e instanceof Object) {
//                             for (r = 0; r < n; ++r) {
//                                 s += " ";
//                             }
//                             for (var l in ((t = "{"), e))
//                                 t +=
//                                     "\n" +
//                                     (0 === s.length ? "" : "" + s) +
//                                     "   [" +
//                                     l +
//                                     "] = " +
//                                     a._dump(e[l], "", i - 1, n);
//                             return t + "\n" + (0 === s.length ? "" : "" + s) + "}";
//                         }
//                         return "Unknown Object Type!";
//                     })(t);
//                 break;
//             case "function":
//                 s += (r = o.length > 0 ? o + " = " : "") + "Function";
//                 break;
//             case "undefined":
//                 s += (r = o.length > 0 ? o + " = " : "") + "Undefined";
//                 break;
//             default:
//                 s += r + " is unknown type!";
//         }
//         return s;
//     };
//     t._instance = null;
//     return t;
// })();
// exports.LoggerImpl = s;


let logLevelNamespace;
let logLevelEnum;

/** 日志级别枚举 */
exports.LoggerImpl = exports.LogLevel = void 0;
(logLevelEnum = logLevelNamespace = exports.LogLevel || (exports.LogLevel = {}))[(logLevelEnum.DEBUG = 1)] = "DEBUG";
logLevelEnum[(logLevelEnum.DUMP = 16)] = "DUMP";
logLevelEnum[(logLevelEnum.WARN = 256)] = "WARN";
logLevelEnum[(logLevelEnum.ERROR = 4096)] = "ERROR";
logLevelEnum[(logLevelEnum.TIME = 65536)] = "TIME";
logLevelEnum[(logLevelEnum.ALL = 69905)] = "ALL";

/** 日志实现类 */
class LoggerImpl {
    constructor() {
        this.logger = console;
        this._level = logLevelNamespace.ALL;
        this._timeLogMap = {};
        this.update();
    }

    /** 单例获取方法 */
    static Instance() {
        return this._instance || (this._instance = new LoggerImpl());
    }

    /** 获取日志级别 */
    get level() {
        return this._level;
    }

    /** 设置日志级别 */
    set level(newLevel) {
        this._level = newLevel;
        this.update();
    }

    /** 附加日志级别 */
    attach(level) {
        if (!this.isValid(level)) {
            this.level = this.level | level;
            this.update();
        }
    }

    /** 分离日志级别 */
    detach(level) {
        if (this.isValid(level)) {
            this.level = this.level ^ level;
            this.update();
        }
    }

    /** 检查日志级别是否合法 */
    isValid(level) {
        return !!(this.level & level);
    }

    /** 更新日志功能 */
    update() {
        this.logger.dump = this.isValid(logLevelNamespace.DUMP)
            ? (cc.sys.isBrowser ? console.log : this.dump.bind(this))
            : function () {};

        this.logger.e = this.isValid(logLevelNamespace.ERROR) ? console.error : function () {};
        this.logger.d = this.isValid(logLevelNamespace.DEBUG) ? console.log : function () {};
        this.logger.w = this.isValid(logLevelNamespace.WARN) ? console.warn : function () {};

        if (this.isValid(logLevelNamespace.TIME)) {
            this.logger.ts = this._kaishi_jishi.bind(this);
            this.logger.te = this._jiesu_jishi.bind(this);
        } else {
            this.logger.ts = function () {};
            this.logger.te = function () {};
        }
    }

    /** 开始计时 */
    _kaishi_jishi(key) {
        this._timeLogMap[key] = Date.now();
    }

    /** 结束计时 */
    _jiesu_jishi(key) {
        if (key in this._timeLogMap) {
            const elapsedTime = Date.now() - this._timeLogMap[key];
            this.logger.d(`${key} use: ${elapsedTime}ms`);
            delete this._timeLogMap[key];
        } else {
            this.logger.e(`log key: ${key} is not exist!`);
        }
    }

    /** Dump日志 */
    dump() {
        if (this.isValid(logLevelNamespace.DUMP)) {
            const output = this._dump(arguments[0], arguments[1], arguments[2], arguments[4]);
            this.logger.d(output);
        }
    }

    /** 递归转储日志信息 */
    _dump(value, label, depth = 10, indent = 0) {
        if (depth < 0) {
            return "...";
        }

        indent += 3;
        let result = "";
        label = label === undefined ? "unknown" : label;
        let spacePadding = " ".repeat(indent);

        switch (typeof value) {
            case "boolean":
                result += `${label.length > 0 ? label + " = " : ""}Boolean(1) ${value ? "TRUE" : "FALSE"}`;
                break;
            case "number":
                result += `${label.length > 0 ? label + " = " : ""}${value}`;
                break;
            case "string":
                result += `${label.length > 0 ? label + " = " : ""}"${value}"`;
                break;
            case "object":
                if (value === null) {
                    result += "NULL(0)";
                } else if (Array.isArray(value)) {
                    result += `Array(${value.length}) [`;
                    for (let i = 0; i < value.length; i++) {
                        result += `\n${spacePadding}[${i}] = ${this._dump(value[i], "", depth - 1, indent)}`;
                    }
                    result += `\n${spacePadding}]`;
                } else {
                    result += `{`;
                    for (let key in value) {
                        result += `\n${spacePadding}[${key}] = ${this._dump(value[key], "", depth - 1, indent)}`;
                    }
                    result += `\n${spacePadding}}`;
                }
                break;
            case "function":
                result += `${label.length > 0 ? label + " = " : ""}Function`;
                break;
            case "undefined":
                result += `${label.length > 0 ? label + " = " : ""}Undefined`;
                break;
            default:
                result += `${label} is unknown type!`;
        }
        return result;
    }
}

LoggerImpl._instance = null;
exports.LoggerImpl = LoggerImpl;
